﻿namespace ApiTemplate.DAL
{
    /// <summary>
    /// 一些基本的数据访问
    /// </summary>
    public class CommonDAL : BaseDAL, ICommonDAL
    {
        public IMapper Mapper { get; set; }

        /// <summary>
        /// 初始化数据表
        /// </summary>
        /// <returns></returns>
        public void InitDataTable()
        {
            Console.WriteLine("开始创建数据库");
            DbContext.DbMaintenance.CreateDatabase();
            string nameSpace = ConfigHelper.GetAppSetting().DataTableNameSpace;

            var basePath = ApplicationEnvironment.ApplicationBasePath;
            var dalFile = Path.Combine(basePath, "ApiTemplate.Entity.dll");
            var assemblysDal = Assembly.LoadFrom(dalFile);
            var typeList = assemblysDal.GetTypes().Where(x => x.Namespace == nameSpace).ToArray();
            foreach (var item in typeList)
            {
                DbContext.CodeFirst.InitTables(item);
                Console.WriteLine($"{item}初始化完成");
            }

            Console.WriteLine("datatable初始化完成");
        }
    }
}
